﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class user2 : Form
    {
        public user2()
        {
            InitializeComponent();
            table();
        }

        private void user2_Load(object sender, EventArgs e)
        {

        }
        //从数据库读取数据并显示在表格控件中
        public void table()
        {
            dataGridView1.Rows.Clear();//清空表格中已有数据
            SQLconnect sqlconnect = new SQLconnect();
            string sql = "select * from t_book";
            IDataReader dc = sqlconnect.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            sqlconnect.SQLclose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号
            string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            string press = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            int number = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
            if (number < 1)
            {
                MessageBox.Show("库存不足，请联系管理员购入");
            }
            else
            {
                string sql = $"insert into t_lend(id,[name],author,press,[uid],bid,[datetime])values('{id}','{name}','{author}','{press}','{Data.UID}','{id}',GETDATE());update t_book set number=number-1 where id='{id}'";
                SQLconnect sqlconnect = new SQLconnect();
                if (sqlconnect.Execute(sql) > 1)//执行了两条sql语句
                {
                    MessageBox.Show($"用户{Data.UName}借出了图书{id}");
                }
            }
            table();
        }
    }
}
